var express = require("express");
var router = express.Router();
const { chatHistoryModule } = require("../module/module");

/* 更新数据库的数据 */
router.post("/getChatHistory", async (req, res) => {
    try {
        let body = req.body;
        await chatHistoryModule.deleteMany({});
        await chatHistoryModule.insertMany(body);
        res.status(200).json({ message: "数据更新成功" });
    } catch (error) {
        console.error("更新数据失败:", error);
        res.status(500).json({ message: "更新数据失败" });
    }
});

/* 获取后端的数据 */
router.get("/getChatHistory", async (req, res) => {
    try {
        const data = await chatHistoryModule.find();
        res.send(data);
    } catch (err) {
        console.log(err);
    }
});

module.exports = router;